package com.mojo.zk.controller;

import com.mojo.zk.configuration.ZkApi;
import com.mojo.response.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
@RestController
@Slf4j
public class ZookeeperController {
    private final ZkApi zkApi;

    @Autowired
    public ZookeeperController(ZkApi zkApi) {
        this.zkApi = zkApi;
    }

    @GetMapping("/create")
    public ResultVO<Boolean> crateNode(@RequestParam String path, @RequestParam String data) {
        log.info("ZookeeperController create node {},{}", path, data);
        boolean ans = zkApi.createNode(path, data);
        return new ResultVO<>(ans);
    }

    @GetMapping("/get")
    public ResultVO<String> getNode(@RequestParam String path) {
        log.info("ZookeeperController get node {}", path);
        String data = zkApi.getData(path, null);
        return new ResultVO<>(data);
    }
}
